﻿
const autoRefresh = (intervalTime, interval, refreshDataGrid, isload = false) => {
    //启动定时任务
    let text = "";
    let intervalId = localStorage.getItem(interval);
    if ((isload && intervalId) || (!isload && !intervalId)) {
        //没有设置间隔时间，默认30s
        let delay = localStorage.getItem(intervalTime);
        if (!delay) {
            delay = 5;
        }
        intervalId = setInterval(() => {
            //获取表格
            if (refreshDataGrid instanceof Function) {
                refreshDataGrid();
            }
        }, Number.parseInt(delay) * 60 * 1000);
        text = "停止自动刷新";
        localStorage.setItem(interval, intervalId);
    } else {
        if (!isload) {
            clearInterval(Number.parseInt(intervalId));
            localStorage.removeItem(interval);
        }
        text = "启用自动刷新";
    }
    return text;
}

const refreshInterval = (intervalTime, interval, refreshDataGrid) => {
    let delay = localStorage.getItem(intervalTime) ?? 5;
    $('<div />').dxForm({
        items: [{
            dataField: 'refreshInterval',
            editorType: 'dxNumberBox',
            isRequired: true,
            requiredMessage: '{0}必填',
            label: { text: "间隔时间（分钟）" },
            editorOptions: {
                value: delay,
                min: 1,
                max: 60,
                showSpinButtons: true,
                format: {
                    type: "fixedPoint",
                    precision: 0
                }
            },
        },
        ],
    }).formPopup({
        title: '设置间隔时间',
        maxWidth: 350,
        maxHeight: 230,
    }).then(result => {
        DevExpress.ui.notify('设置成功', 'success');
        //存localStorage
        // setItem   getItem  removeItem clear
        if (localStorage.getItem(intervalTime)) {
            localStorage.removeItem(intervalTime);
        }
        localStorage.setItem(intervalTime, result.refreshInterval);
        let intervalId = localStorage.getItem(interval)
        if (intervalId) {
            //如果已经有定时器，清掉，重新创建一个定时器
            clearInterval(Number.parseInt(intervalId));
            localStorage.removeItem(interval);
            intervalId = setInterval(() => {
                if (refreshDataGrid instanceof Function) {
                    refreshDataGrid();
                }
                localStorage.setItem(interval, intervalId);
            }, Number.parseInt(result.refreshInterval) * 60 * 1000);
        }
    });
}